Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year1.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 364)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.75934 12.77112 12.78267 12.79399 12.80510 12.81598 12.82666 12.83713
##   [9] 12.84740 12.85747 12.86734 12.87702 12.88652 12.89583 12.90497 12.91390
##  [17] 12.92259 12.93106 12.93929 12.94729 12.95506 12.96260 12.96991 12.97699
##  [25] 12.98384 12.99046 12.99685 13.00301 13.00894 13.01465 13.02013 13.02538
##  [33] 13.03040 13.03520 13.03978 13.04412 13.04819 13.05193 13.05536 13.05848
##  [41] 13.06131 13.06386 13.06614 13.06816 13.06994 13.07147 13.07279 13.07389
##  [49] 13.07479 13.07550 13.07603 13.07639 13.07659 13.07666 13.07658 13.07639
##  [57] 13.07608 13.07570 13.07525 13.07472 13.07406 13.07326 13.07230 13.07114
##  [65] 13.06977 13.06815 13.06626 13.06408 13.06158 13.05873 13.05551 13.05134
##  [73] 13.04573 13.03880 13.03069 13.02152 13.01143 13.00052 12.98895 12.97682
##  [81] 12.96428 12.95144 12.93844 12.92540 12.91246 12.89972 12.88734 12.87543
##  [89] 12.86412 12.85353 12.84380 12.83506 12.82484 12.81102 12.79421 12.77504
##  [97] 12.75412 12.73207 12.70950 12.68704 12.66530 12.64490 12.62646 12.61060
## [105] 12.59793 12.58907 12.58336 12.57964 12.57776 12.57759 12.57899 12.58183
## [113] 12.58598 12.59129 12.59763 12.60486 12.61285 12.62147 12.63056 12.64001
## [121] 12.64967 12.65940 12.66908 12.67856 12.68771 12.69639 12.70446 12.71180
## [129] 12.71819 12.72374 12.72882 12.73380 12.73902 12.74485 12.75165 12.75742
## [137] 12.76029 12.76085 12.75969 12.75738 12.75450 12.75165 12.74940 12.74833
## [145] 12.74903 12.75208 12.75807 12.76695 12.77801 12.79082 12.80495 12.81997
## [153] 12.83545 12.85094 12.86603 12.88028 12.89564 12.91402 12.93490 12.95775
## [161] 12.98206 13.00731 13.03297 13.05853 13.08346 13.10725 13.12937 13.14930
## [169] 13.16934 13.19152 13.21494 13.23869 13.26186 13.28356 13.30288 13.32217
## [177] 13.34414 13.36839 13.39451 13.42212 13.45082 13.48020 13.50988 13.53946
## [185] 13.56854 13.59673 13.62362 13.64883 13.67195 13.69259 13.71035 13.72484
## [193] 13.73566 13.74242 13.74471 13.74214 13.73511 13.72469 13.71148 13.69609
## [201] 13.67912 13.66117 13.64285 13.62476 13.60220 13.57156 13.53517 13.49532
## [209] 13.45433 13.41449 13.37812 13.34751 13.31344 13.26800 13.21546 13.16013
## [217] 13.10629 13.05823 13.02024 12.98730 12.95190 12.91453 12.87570 12.83591
## [225] 12.79566 12.75545 12.71578 12.67716 12.64009 12.60507 12.57260 12.54403
## [233] 12.51964 12.49829 12.47890 12.46033 12.44148 12.42123 12.39847 12.37208
## [241] 12.34330 12.31423 12.28507 12.25602 12.22729 12.19907 12.17158 12.14502
## [249] 12.11958 12.09547 12.07290 12.05206 12.02975 12.00379 11.97592 11.94787
## [257] 11.92138 11.89818 11.88002 11.86130 11.83660 11.80792 11.77728 11.74667
## [265] 11.71810 11.69359 11.67513 11.66473 11.66164 11.66298 11.66760 11.67432
## [273] 11.68198 11.68941 11.69545 11.70670 11.72912 11.76058 11.79893 11.84205
## [281] 11.88780 11.93403 11.97862 12.01941 12.05429 12.08110 12.09772 12.11168
## [289] 12.13031 12.15113 12.17168 12.18948 12.20206 12.20696 12.20509 12.19944
## [297] 12.19062 12.17924 12.16591 12.15124 12.13584 12.12032 12.10530 12.08681
## [305] 12.06191 12.03274 12.00145 11.97018 11.94107 11.91627 11.89019 11.85681
## [313] 11.81760 11.77404 11.72762 11.67980 11.63207 11.58590 11.54278 11.50417
## [321] 11.47155 11.44642 11.42837 11.41495 11.40454 11.39550 11.38620 11.37501
## [329] 11.36030 11.34449 11.33075 11.31860 11.30753 11.29706 11.28669 11.27592
## [337] 11.26427 11.25123 11.23770 11.22483 11.21248 11.20052 11.18881 11.17722
## [345] 11.16561 11.15417 11.14316 11.13253 11.12224 11.11227 11.10257 11.09311
## [353] 11.08385 11.07476 11.06581 11.05694 11.04814 11.03904 11.02943 11.01948
## [361] 11.00938 10.99928 10.98936 10.97980
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year1.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 364)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.58808 12.58226 12.57637 12.57050 12.56469 12.55901 12.55351 12.54826
##   [9] 12.54331 12.53872 12.53455 12.53086 12.52771 12.52516 12.52327 12.52193
##  [17] 12.52097 12.52036 12.52009 12.52013 12.52045 12.52103 12.52185 12.52287
##  [25] 12.52408 12.52545 12.52696 12.52858 12.53028 12.53205 12.53385 12.53567
##  [33] 12.53748 12.53924 12.54095 12.54258 12.54444 12.54688 12.54985 12.55333
##  [41] 12.55729 12.56170 12.56653 12.57175 12.57734 12.58325 12.58947 12.59595
##  [49] 12.60268 12.60963 12.61676 12.62404 12.63144 12.63894 12.64651 12.65411
##  [57] 12.66172 12.66930 12.67683 12.68428 12.69161 12.69881 12.70583 12.71265
##  [65] 12.71924 12.72557 12.73161 12.73734 12.74271 12.74770 12.75229 12.75596
##  [73] 12.75827 12.75927 12.75903 12.75761 12.75506 12.75144 12.74682 12.74125
##  [81] 12.73480 12.72752 12.71947 12.71072 12.70132 12.69133 12.68081 12.66982
##  [89] 12.65843 12.64668 12.63465 12.62238 12.60994 12.59740 12.58480 12.57221
##  [97] 12.55969 12.54729 12.53509 12.52313 12.51147 12.50019 12.48932 12.47895
## [105] 12.46912 12.45989 12.45125 12.44313 12.43557 12.42859 12.42221 12.41645
## [113] 12.41135 12.40692 12.40319 12.40018 12.39792 12.39643 12.39573 12.39585
## [121] 12.39681 12.39863 12.40135 12.40498 12.40955 12.41508 12.42159 12.42911
## [129] 12.43767 12.44729 12.45798 12.46979 12.48272 12.49882 12.51938 12.54332
## [137] 12.56956 12.59700 12.62456 12.65114 12.67568 12.69707 12.71691 12.73744
## [145] 12.75852 12.78004 12.80187 12.82387 12.84592 12.86790 12.88967 12.91111
## [153] 12.93210 12.95249 12.97442 12.99973 13.02791 13.05845 13.09084 13.12456
## [161] 13.15910 13.19396 13.22861 13.26255 13.29525 13.32622 13.35494 13.38089
## [169] 13.40356 13.42245 13.43883 13.45435 13.46899 13.48273 13.49556 13.50746
## [177] 13.51843 13.52845 13.53750 13.54558 13.55266 13.55874 13.56380 13.56783
## [185] 13.57082 13.57274 13.57359 13.57336 13.57203 13.56958 13.56601 13.56130
## [193] 13.55543 13.54840 13.54018 13.53077 13.52016 13.50832 13.49459 13.47842
## [201] 13.45999 13.43948 13.41705 13.39290 13.36719 13.34010 13.31181 13.28250
## [209] 13.25233 13.22149 13.19016 13.15851 13.12671 13.09495 13.06340 13.03223
## [217] 13.00163 12.96921 12.93309 12.89424 12.85359 12.81211 12.77074 12.73044
## [225] 12.69214 12.65682 12.62225 12.58590 12.54818 12.50951 12.47031 12.43100
## [233] 12.39199 12.35371 12.31657 12.28100 12.24740 12.21621 12.18531 12.15262
## [241] 12.11859 12.08364 12.04823 12.01279 11.97776 11.94359 11.91072 11.87959
## [249] 11.85063 11.82430 11.80103 11.78126 11.76543 11.75399 11.74714 11.74458
## [257] 11.74595 11.75089 11.75905 11.77007 11.78360 11.79928 11.81675 11.83568
## [265] 11.85568 11.87643 11.89754 11.91869 11.93950 11.95962 11.97870 11.99638
## [273] 12.01231 12.02963 12.05122 12.07640 12.10445 12.13467 12.16634 12.19876
## [281] 12.23123 12.26304 12.29347 12.32183 12.34741 12.36949 12.38738 12.40037
## [289] 12.40774 12.40971 12.40735 12.40123 12.39196 12.38011 12.36628 12.35104
## [297] 12.33499 12.31872 12.30280 12.28783 12.27440 12.26009 12.24253 12.22234
## [305] 12.20016 12.17660 12.15230 12.12788 12.10398 12.08122 12.05720 12.02945
## [313] 11.99843 11.96465 11.92858 11.89070 11.85150 11.81147 11.77109 11.73084
## [321] 11.69120 11.65267 11.61573 11.58086 11.54854 11.51926 11.49350 11.47175
## [329] 11.45450 11.44021 11.42705 11.41506 11.40424 11.39461 11.38620 11.37902
## [337] 11.37310 11.36845 11.36509 11.36305 11.36234 11.36298 11.36500 11.36841
## [345] 11.37323 11.37945 11.38701 11.39594 11.40625 11.41794 11.43103 11.44552
## [353] 11.46142 11.47874 11.49750 11.51770 11.53934 11.56245 11.58703 11.61308
## [361] 11.64063 11.66967 11.70022 11.73228
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year1.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 364)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.24481 11.28639 11.32725 11.36738 11.40676 11.44540 11.48327 11.52037
##   [9] 11.55669 11.59221 11.62693 11.66083 11.69391 11.72615 11.75754 11.78807
##  [17] 11.81773 11.84652 11.87441 11.90140 11.92748 11.95264 11.97685 12.00013
##  [25] 12.02248 12.04392 12.06446 12.08413 12.10294 12.12090 12.13804 12.15436
##  [33] 12.16988 12.18462 12.19860 12.21183 12.22433 12.23610 12.24718 12.25757
##  [41] 12.26729 12.27636 12.28480 12.29203 12.29758 12.30152 12.30398 12.30506
##  [49] 12.30485 12.30347 12.30102 12.29761 12.29332 12.28828 12.28259 12.27635
##  [57] 12.26966 12.26263 12.25536 12.24795 12.24052 12.23317 12.22599 12.21910
##  [65] 12.21098 12.20031 12.18743 12.17270 12.15648 12.13912 12.12098 12.10242
##  [73] 12.08378 12.06543 12.04772 12.03101 12.01566 12.00201 11.98685 11.96704
##  [81] 11.94307 11.91544 11.88463 11.85114 11.81546 11.77807 11.73948 11.70016
##  [89] 11.66062 11.62133 11.58280 11.54551 11.50996 11.47663 11.44602 11.41862
##  [97] 11.39491 11.37540 11.36056 11.34800 11.33515 11.32225 11.30951 11.29718
## [105] 11.28549 11.27468 11.26497 11.25660 11.24981 11.24482 11.24187 11.24120
## [113] 11.24303 11.24865 11.25886 11.27314 11.29098 11.31186 11.33527 11.36069
## [121] 11.38762 11.41554 11.44394 11.47230 11.50011 11.52685 11.55202 11.57510
## [129] 11.59876 11.62541 11.65414 11.68405 11.71424 11.74381 11.77185 11.79979
## [137] 11.82947 11.86058 11.89283 11.92593 11.95957 11.99346 12.02729 12.06078
## [145] 12.09361 12.12550 12.15615 12.18643 12.21728 12.24851 12.27990 12.31127
## [153] 12.34240 12.37310 12.40316 12.43238 12.46302 12.49696 12.53342 12.57162
## [161] 12.61081 12.65020 12.68902 12.72651 12.76188 12.79437 12.82320 12.84760
## [169] 12.87114 12.89706 12.92405 12.95083 12.97610 12.99857 13.01694 13.03199
## [177] 13.04537 13.05715 13.06741 13.07619 13.08357 13.08960 13.09435 13.09789
## [185] 13.10027 13.10156 13.10183 13.10112 13.09952 13.09708 13.09387 13.08994
## [193] 13.08537 13.08021 13.07452 13.06838 13.06026 13.04910 13.03563 13.02060
## [201] 13.00475 12.98883 12.97357 12.95972 12.94809 12.93839 12.92975 12.92130
## [209] 12.91215 12.90145 12.88831 12.87185 12.85577 12.84317 12.83230 12.82142
## [217] 12.80878 12.79264 12.77125 12.74604 12.71960 12.69189 12.66290 12.63258
## [225] 12.60092 12.56789 12.53347 12.49761 12.46031 12.42152 12.38123 12.33490
## [233] 12.27962 12.21783 12.15195 12.08440 12.01761 11.95402 11.89604 11.84610
## [241] 11.79498 11.73352 11.66420 11.58950 11.51193 11.43396 11.35809 11.28680
## [249] 11.22258 11.16791 11.12530 11.09722 11.07890 11.06378 11.05168 11.04240
## [257] 11.03576 11.03156 11.02961 11.03662 11.05720 11.08806 11.12589 11.16739
## [265] 11.20925 11.24817 11.28085 11.30399 11.33350 11.38246 11.44332 11.50850
## [273] 11.57045 11.62159 11.65437 11.67958 11.71188 11.74954 11.79081 11.83395
## [281] 11.87722 11.91888 11.95719 11.99040 12.01677 12.03456 12.04203 12.03693
## [289] 12.02056 11.99649 11.96830 11.93958 11.91389 11.89481 11.87231 11.83625
## [297] 11.79011 11.73737 11.68148 11.62593 11.57418 11.52970 11.49596 11.46817
## [305] 11.43942 11.41004 11.38035 11.35065 11.32129 11.29257 11.26150 11.22563
## [313] 11.18612 11.14409 11.10068 11.05704 11.01429 10.97359 10.93605 10.90283
## [321] 10.87506 10.85389 10.83606 10.81817 10.80103 10.78549 10.77239 10.76256
## [329] 10.75684 10.75604 10.75980 10.76721 10.77734 10.78928 10.80210 10.81489
## [337] 10.82673 10.83670 10.84630 10.85754 10.87022 10.88419 10.89925 10.91523
## [345] 10.93196 10.94953 10.96817 10.98791 11.00879 11.03084 11.05408 11.07856
## [353] 11.10429 11.13132 11.15967 11.18937 11.22046 11.25275 11.28608 11.32048
## [361] 11.35600 11.39270 11.43060 11.46976
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year1.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")